package com.zmn.plat.business.impl.msg.conf;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.collection.CollectionUtil;
import com.zmn.mcc.common.dto.staff.StaffDRO;
import com.zmn.mcc.dubbo.interfaces.staff.StaffListRemoteService;
import com.zmn.plat.business.interfaces.msg.conf.MsgConfBService;
import com.zmn.plat.dubbo.utils.DubboConsts;
import com.zmn.plat.model.entity.msg.receive.position.conf.MsgReceivePositionConf;
import com.zmn.plat.model.entity.msg.receive.position.conf.MsgReceivePositionConfQuery;
import com.zmn.plat.model.entity.msg.receive.role.conf.MsgReceiveRoleConf;
import com.zmn.plat.model.entity.msg.receive.role.conf.MsgReceiveRoleConfQuery;
import com.zmn.plat.model.entity.msg.receive.staff.conf.MsgReceiveStaffConf;
import com.zmn.plat.model.entity.msg.receive.staff.conf.MsgReceiveStaffConfQuery;
import com.zmn.plat.model.vo.msg.receive.staff.MsgStaffSimpleVO;
import com.zmn.plat.services.interfaces.msg.receive.position.conf.MsgReceivePositionConfService;
import com.zmn.plat.services.interfaces.msg.receive.role.conf.MsgReceiveRoleConfService;
import com.zmn.plat.services.interfaces.msg.receive.staff.conf.MsgReceiveStaffConfService;

/**
 * 消息配置管理BServiceImpl
 *
 * @author linfeng
 * @since 2020/06/01 16:15
 */
@Service
public class MsgConfBServiceImpl implements MsgConfBService {
    private Logger logger = LoggerFactory.getLogger(MsgConfBServiceImpl.class);

    @Resource
    MsgReceivePositionConfService msgReceivePositionConfService;

    @Resource
    MsgReceiveRoleConfService msgReceiveRoleConfService;

    @Resource
    MsgReceiveStaffConfService msgReceiveStaffConfService;

    @Reference(version = DubboConsts.INTERFACE_VERSION, check = false)
    StaffListRemoteService staffListRemoteService;

    @Override
    public List<MsgStaffSimpleVO> listEligibleStaffgeByMsgId(Integer msgId) {
        MsgReceivePositionConfQuery msgReceivePositionConfQuery = new MsgReceivePositionConfQuery();
        msgReceivePositionConfQuery.setMsgId(msgId);
        List<MsgReceivePositionConf> msgReceivePositionConfs = msgReceivePositionConfService.listByQuery(msgReceivePositionConfQuery);
        List<StaffDRO> allStaffDRO = new ArrayList<>();
        if (CollectionUtil.isNotNullOrEmpty(msgReceivePositionConfs)) {
            List<Integer> deptIds = msgReceivePositionConfs.stream().map(e -> e.getOrgId()).collect(Collectors.toList());
            ResponseDTO<List<StaffDRO>> data = staffListRemoteService.listStaffByDeptIds(deptIds, GlobalConsts.YES);
            List<StaffDRO> staffDROS = new ArrayList<>();
            if (data.isSuccess()) {
                staffDROS = data.getData();
            }

            allStaffDRO.addAll(staffDROS);
        }

        MsgReceiveRoleConfQuery msgReceiveRoleConfQuery = new MsgReceiveRoleConfQuery();
        msgReceiveRoleConfQuery.setMsgId(msgId);
        List<MsgReceiveRoleConf> msgReceiveRoleConfs = msgReceiveRoleConfService.listByQuery(msgReceiveRoleConfQuery);
        if (CollectionUtil.isNotNullOrEmpty(msgReceiveRoleConfs)) {
            List<Integer> roleIds = msgReceiveRoleConfs.stream().map(e -> e.getRoleId()).collect(Collectors.toList());
            ResponseDTO<List<StaffDRO>> data = staffListRemoteService.listStaffByRoleIds(roleIds, GlobalConsts.YES);
            List<StaffDRO> staffDROS = new ArrayList<>();
            if (data.isSuccess()) {
                staffDROS = data.getData();
            }

            allStaffDRO.addAll(staffDROS);
        }

        MsgReceiveStaffConfQuery msgReceiveStaffConfQuery = new MsgReceiveStaffConfQuery();
        msgReceiveStaffConfQuery.setMsgId(msgId);
        List<MsgReceiveStaffConf> msgReceiveStaffConfs = msgReceiveStaffConfService.listByQuery(msgReceiveStaffConfQuery);
        if (CollectionUtil.isNotNullOrEmpty(msgReceiveStaffConfs)) {
            List<Integer> staffIds = msgReceiveStaffConfs.stream().map(e -> e.getStaffId()).collect(Collectors.toList());
            ResponseDTO<List<StaffDRO>> data = staffListRemoteService.listStaffByStaffIds(staffIds, GlobalConsts.YES);
            List<StaffDRO> staffDROS = new ArrayList<>();
            if (data.isSuccess()) {
                staffDROS = data.getData();
            }

            allStaffDRO.addAll(staffDROS);
        }

        if (CollectionUtil.isNotNullOrEmpty(allStaffDRO)) {
            List<StaffDRO> distinctStaffDroList = allStaffDRO.stream().distinct().collect(Collectors.toList());
            List<MsgStaffSimpleVO> msgStaffSimpleVOS = distinctStaffDroList.stream().map(staffDRO -> {
                MsgStaffSimpleVO msgStaffSimpleVO = new MsgStaffSimpleVO();
                BeanUtils.copyProperties(staffDRO, msgStaffSimpleVO);
                return msgStaffSimpleVO;
            }).collect(Collectors.toList());

            return msgStaffSimpleVOS;
        }

        return Collections.EMPTY_LIST;
    }

    @Override
    public List<MsgStaffSimpleVO> listReceiveStaffSimpleVO(List<Integer> staffIds) {

        List<MsgStaffSimpleVO> staffSimpleVOS = new ArrayList<>();
        if (CollectionUtil.isNullOrEmpty(staffIds)) {
            return staffSimpleVOS;
        }

        // mcc 查询员工相关的id 如钉钉id
        ResponseDTO<List<StaffDRO>> data = staffListRemoteService.listStaffByStaffIds(staffIds, GlobalConsts.YES);
        if (!data.isSuccess()) {
            return staffSimpleVOS;
        }

        List<StaffDRO> staffs = data.getData();
        if (CollectionUtil.isNullOrEmpty(staffs)) {
            return staffSimpleVOS;
        }

        staffs.forEach(staff -> {
            MsgStaffSimpleVO staffSimpleVO = new MsgStaffSimpleVO();
            staffSimpleVO.setStaffId(staff.getStaffId());
            staffSimpleVO.setDTalkId(staff.getDTalkId());
            staffSimpleVO.setMobile(staff.getMobile());
            staffSimpleVO.setStaffNumber(staff.getStaffNumber());
            staffSimpleVOS.add(staffSimpleVO);
        });

        return staffSimpleVOS;
    }
}
